$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址

	if($("#selected_ops").val() == ""){
		$("#dimension-showspan").css("display","block");
	}else{
		$("#dimension-showspan").css("display","none");
	}
	
	//选中右移
    var $chooseing_li = $("#dimension-chooseing li");
	$chooseing_li.live('click', function() {
		var mychoice = this.innerHTML;
		$(this).remove();
	    var ul = document.getElementById("dimension-selected");
		$(ul).append("<li>" + mychoice + "<input type='button' class='movedel'/><input type='button' class='movedown'/><input type='button' class='moveup'/></li>");
		if($("#selected_ops").val() == ""){
			$("#selected_ops").val(mychoice);
		}else{
			$("#selected_ops").val($("#selected_ops").val()+"-"+mychoice);
		}
		if($("#selected_ops").val() == ""){
			$("#dimension-showspan").css("display","block");
		}else{
			$("#dimension-showspan").css("display","none");
		}
	});
	
    //选中左移
	$('#dimension-selected li input.movedel').live('click', function() {	
		var mychoice = $(this).parents("li").text();
		$(this).parents("li").remove();
	    var ul = document.getElementById("dimension-chooseing");
		$(ul).append("<li>" + mychoice + "</li>");
		//获取content值
		var selectedArr = $("#selected_ops").val().split("-");
		for(var i=0;i<selectedArr.length;i++){
			if(selectedArr[i] == mychoice){
				selectedArr.splice(i,1);
			}
		} 
		if(selectedArr.length !=0){
			for(var j=0;j<selectedArr.length;j++){
				if(j == 0){
					$("#selected_ops").val(selectedArr[0]);
				}else{
					$("#selected_ops").val($("#selected_ops").val()+"-"+selectedArr[j]);
				}
			} 
		}else{
			$("#selected_ops").val("");
		}
		if($("#selected_ops").val() == ""){
			$("#dimension-showspan").css("display","block");
		}else{
			$("#dimension-showspan").css("display","none");
		}
	});
	
	//上移
	$('#dimension-selected li input.moveup').live('click', function() {	
        var li = $(this).parents("li");
        if (li.index() != 0) {
			//li.fadeOut().fadeIn();
			li.prev().before(li);
		}
    });    
		
	//下移
    var $down = $('#dimension-selected li input.movedown');
    var len = $down.length;
    $down.live('click', function() {	
        var li = $(this).parents("li");
        //if (li.index() != len) {
			//li.fadeOut().fadeIn();
            li.next().after(li);
        //}
    });
    
	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	
	$(".btn-save").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = isExist();
		if(falg){
			return false;
		}
		if($("#selected_ops").val() == ""){
			bootbox.alert("请选择维度方案！");
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	

	$("#name").blur(function(){
		isExist();
	});
});